package com.bocloud.cmp.dao.app.impl;

import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;
import com.bocloud.cmp.dao.app.RegistryImageInfoDao;
import com.bocloud.cmp.entity.app.resource.RegistryImageInfo;
import com.bocloud.common.model.Param;
import com.bocloud.common.utils.ListTool;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.JdbcGenericDao;
import com.bocloud.database.utils.SQLHelper;
/**
 * 
 * @author Zaney
 * @data:2017年3月14日
 * @describe:
 */
@Repository("registryImageInfoDao")
public class RegistryImageInfoDaoImpl extends JdbcGenericDao<RegistryImageInfo, Long> implements RegistryImageInfoDao {
	@Override
	public List<RegistryImageInfo> selectRepositorySoftware(int page, int rows,List<Param> params, String deptId ) throws Exception {
		String sql = "select repository_software_info.repository_id,repository_software_info.namespace,repository_software_info.software_id,software.* "
				+ "from repository_software_info  LEFT JOIN software  on repository_software_info.software_id = software.id "
				+ "WHERE software.is_deleted = 0 ";
		if (null != deptId) {
			sql += "and (software.dept_id in (:deptId) or repository_software_info.namespace = 'library')";
		}
		sql = SQLHelper.buildRawSql(sql, page, rows, params, null, null);
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		paramMap.put("deptId", deptId);
		return this.list(RegistryImageInfo.class, sql, paramMap);
	}
	
	@Override
	public List<RegistryImageInfo> selectRepositorySoftware(Long repositoryId) throws Exception {
		String sql = "select r.id reg_id,r.namespace,r.software_id,r.repository_id,s.* from repository_software_info r LEFT JOIN software s on r.software_id = s.id "
				+ "WHERE s.is_deleted = 0 and r.repository_id = :repositoryId";
		Map<String, Object> params = MapTools.simpleMap("repositoryId", repositoryId);
		return this.list(RegistryImageInfo.class, sql, params);
	}
	@Override
	public int count(List<Param> params, String deptId) throws Exception {
		String sql = "select count(1) from repository_software_info LEFT JOIN software  "
				+ "on repository_software_info.software_id = software.id "
				+ "WHERE software.is_deleted = 0 ";
		if (null != deptId) {
			sql += "and (software.dept_id in (:deptId) or repository_software_info.namespace = 'library')";
		}
		sql = SQLHelper.buildRawSql(sql, params, null, null);
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		paramMap.put("deptId", deptId);
		return this.countQuery(sql, paramMap).intValue();
	}

	@Override
	public RegistryImageInfo getByImageId(Long imageId) throws Exception {
		String sql = "select rs.namespace,rs.software_id,rs.repository_id,r.name as registry_name,s.* from repository_software_info rs "
				+ "LEFT JOIN software s on rs.software_id = s.id "
				+ "LEFT JOIN registry r on rs.repository_id = r.id  "
				+ "WHERE s.is_deleted = 0 and rs.software_id = :softwareId";
		Map<String, Object> params = MapTools.simpleMap("softwareId", imageId);
		List<RegistryImageInfo> registryImageInfos = this.list(RegistryImageInfo.class, sql, params);
		if (!ListTool.isEmpty(registryImageInfos)) {
			return registryImageInfos.get(0);
		}
		return null;
	}
	
	@Override
	public List<RegistryImageInfo> list(List<Param> params, Map<String, String> sorter, String deptId) throws Exception {
		String sql = "select repository_software_info.repository_id,repository_software_info.namespace,repository_software_info.software_id,"
				+ "software.name,software.version "
				+ "from repository_software_info  LEFT JOIN software  on repository_software_info.software_id = software.id "
				+ "WHERE software.is_deleted = 0 ";
		if (null != deptId) {
			sql += "and (software.dept_id in (:deptId) or repository_software_info.namespace = 'library')";
		}
		sql = SQLHelper.buildRawSql(sql, params, null, null);
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		paramMap.put("deptId", deptId);
		return this.list(RegistryImageInfo.class, sql, paramMap);
	}
	
}
